﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using DevExpress.XtraCharts;

namespace NotebookS2.Reports.Carousel
{
    public partial class ProductStats : DevExpress.XtraEditors.XtraForm
    {
        public ProductStats()
        {
            InitializeComponent();
        }

        public MainMenu MainForm { get; set; }
        public string ConnectionString { get; set; }
        public double ProductCode { get; set; }

        private void ProductStats_Load(object sender, EventArgs e)
        {
            SqlConnection oConnection = new SqlConnection();
            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();


            #region productname combobox

            //SqlCommand oCommandP = new SqlCommand("SELECT * FROM STOCKCARD", oConnection);
            //oCommandP.Connection = oConnection;

            //SqlDataAdapter odt = new datat

            #endregion

            #region Bind Products Stocks

            
            SqlCommand oCommand = new SqlCommand("SELECT ItemCode, Description FROM ItemMaster WHERE IsActive <> 0 ORDER BY Description ASC", oConnection);
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            int iRow = dt.Rows.Count;
            int oRowNum = 0;

            foreach(DataRow oRow in dt.Rows)
            {
               oRowNum = oRowNum + 1;

               string selectedItem = oRow["ItemCode"].ToString();
               

               if(oRowNum == ProductCode)
               {
                   #region inventory

                   SqlCommand oCommand1 = new SqlCommand();
                   oCommand1.Connection = oConnection;
                   oCommand1.CommandType = CommandType.StoredProcedure;
                   oCommand1.CommandText = "CAROUSEL_INV_LAYOUT";
                   oCommand1.Parameters.AddWithValue("@PRODUCT", selectedItem);

                   SqlDataAdapter oAdapter1 = new SqlDataAdapter();
                   DataTable dt1 = new DataTable();

                   oAdapter1.SelectCommand = oCommand1;
                   oAdapter1.Fill(dt1);

                   chartControl1.DataSource = dt1;
                   chartControl1.SeriesDataMember = "LotNumber";
                  
                   chartControl1.SeriesTemplate.ArgumentDataMember = "WhsName";
                   chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Qty" });
                   chartControl1.Refresh();

                   #endregion

                   #region sales invoice

                   SqlCommand oCommand2 = new SqlCommand();
                   oCommand2.Connection = oConnection;
                   oCommand2.CommandType = CommandType.StoredProcedure;
                   oCommand2.CommandText = "SP_RPT_INVOICES";
                   oCommand2.Parameters.AddWithValue("@FROM", "10/1/13");
                   oCommand2.Parameters.AddWithValue("@TO", "11/8/13");
                   oCommand2.Parameters.AddWithValue("@ITEMCODE", selectedItem);

                   SqlDataAdapter oAdapter2 = new SqlDataAdapter();
                   DataTable dt2 = new DataTable();

                   oAdapter2.SelectCommand = oCommand2;
                   oAdapter2.Fill(dt2);

                   int iRow2 = dt2.Rows.Count;
                   if (iRow2 > 0)
                   {
                       DataRow oRow2 = dt2.Rows[0];

                       lblProductName.Text = oRow2["ItemDesc"].ToString();

                       chartControl2.DataSource = dt2;
                       chartControl2.SeriesDataMember = "ItemDesc";
                       chartControl2.SeriesTemplate.ArgumentDataMember = "Month_Name";
                       chartControl2.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "TotalNetAmount" });

                       Series series1 = new Series("Product Quantity", ViewType.Bar);
                       chartControl2.Series.Add(series1);
                       series1.ArgumentDataMember = "Month_Name";
                       
                       series1.ValueDataMembers.AddRange(new string[] { "TotalQuantity" });
                       chartControl2.Refresh();
                   }
                  

                   #endregion
               }
            }

            #endregion

        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void labelControl4_Resize(object sender, EventArgs e)
        {

        }

        private void ProductStats_Resize(object sender, EventArgs e)
        {
            lblProductName.Left = this.Width - 300;
        }

        private void chartControl2_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {
            // Get the value of your point (Age in your case)
            var pointValue = e.SeriesPoint.Values[0];

            // You can get the argument text using e.SeriesPoint.Argument
            // Set the label text of your point
            e.LabelText = "value is " + pointValue;

            if (e.SeriesPoint.Values[0] < 1)
            {
                e.LabelText = "Critical value";
            }
        }
    }
}